package com.briup.jz.service.impl;

import com.briup.jz.bean.BaseConfig;

import com.briup.jz.dao.BaseConfigMapper;
import com.briup.jz.service.IBaseConfigService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;
import com.briup.jz.bean.BaseConfigExample;
import javax.annotation.Resource;
import java.util.List;

@Service  //表明为service层  交给ioc管理
public class BaseConfigServiceImpl implements IBaseConfigService {


    //依赖注入
    @Resource
    private BaseConfigMapper baseConfigMapper;


    @Override
    public void saveOrUpdate(BaseConfig baseConfig) throws CustomerException {
        //判断id是否存在
        if (baseConfig.getId()!=null){
            //更新操作
            baseConfigMapper.updateByPrimaryKey(baseConfig);
        }else {
            //执行插入操作,要求名字不能相同
            BaseConfigExample example=new BaseConfigExample();
            //配置参数
            example.createCriteria().andNameEqualTo(baseConfig.getName());
            List<BaseConfig> list=baseConfigMapper.selectByExample(example);
            if (list.size()>0){
                //说明有同名的
                throw new CustomerException("有同类栏目");
            }

            baseConfigMapper.insert(baseConfig);

        }

    }




    @Override
    public List<BaseConfig> query(String name) {

      BaseConfigExample example= new BaseConfigExample();
      if (name!=null){
          example.createCriteria().andNameLike("%"+name+"%");
      }
      return baseConfigMapper.selectByExample(example);
    }

    @Override
    public void deleteById(Long id) throws CustomerException {

        //删除之前首先判断这个id对象是否存在
        BaseConfig baseConfig=baseConfigMapper.selectByPrimaryKey(id);

        if (baseConfig!=null){
            throw new CustomerException("对象不存在");
        }
            baseConfigMapper.deleteByPrimaryKey(id);

    }


    @Override
    public List<BaseConfig> queryAll() {
      BaseConfigExample example= new BaseConfigExample();
      return  baseConfigMapper.selectByExample(example);
    }
}
